package org.xx.armory.commons;

public abstract class StringTokenParser {
    private final String content;
    private int pos;

    public StringTokenParser(
            String content
    ) {
        this.content = content != null ? content : "";
        this.pos = 0;
    }

    public final boolean hasNext() {
        return this.pos < this.content.length();
    }

    public abstract String next();

    protected final int readChar() {
        if (this.pos < this.content.length()) {
            return this.content.codePointAt(this.pos++);
        } else {
            return -1;
        }
    }

    protected final void putBack() {
        if (this.pos > 0) {
            --this.pos;
        }
    }

    protected final void setIllegalState() {
        if (this.pos < this.content.length() - 1) {
            throw new IllegalStateException("cannot parse token: " + this.content + ", pos=" + (pos - 1));
        } else {
            throw new IllegalStateException("cannot parse token at end: " + this.content);
        }
    }
}
